package cn.edu.hit.graphdata;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by Wang on 2018/11/16.
 */
@Component
public class AppConfigure implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
    @Autowired
    private ApplicationArguments applicationArguments;

    @Override
    public void customize(ConfigurableServletWebServerFactory configurableServletWebServerFactory) {
        if (applicationArguments.containsOption("usePort")) {
            List<String> portString = applicationArguments.getOptionValues("usePort");
//            System.out.println(portString);
            if (portString.size() > 0 && StringUtils.isNumeric(portString.get(0))) {
                configurableServletWebServerFactory.setPort(Integer.parseInt(portString.get(0)));
            }
        }
//        configurableServletWebServerFactory.setPort();
    }

    public void setApplicationArguments(ApplicationArguments applicationArguments) {
        this.applicationArguments = applicationArguments;
    }
}
